package com.rentong.json;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

/**
 * @author ZXY
 * @ClassName: JsonUtils
 * @Description: json 对象转换工具类
 * @date 2016/6/17 13:26
 */
public class JsonUtils {

    private static ObjectMapper objectMapper = new JsonMapper();

    /**
     * @param json
     * @param clazz 对象的 class
     * @return 对象
     * @throws IOException
     * @Description: json 转换成对象
     */
    public static <T> T readValue(String json, Class<T> clazz) throws IOException {
        return objectMapper.readValue(json, clazz);
    }

    /**
     * @param json
     * @param name 属性名称
     * @return 属性值
     * @throws IOException
     * @Description: 获取json 的属性值
     */
    public static String readValueByName(String json, String name) throws IOException {
        Map<?, ?> map = objectMapper.readValue(json, Map.class);
        return map.get(name).toString();
    }

    /**
     * @param json
     * @return map
     * @throws IOException
     * @Description: 获取json 的属性 map
     */
    public static Map<?, ?> readMap(String json) throws IOException {
        Map<?, ?> map = objectMapper.readValue(json, Map.class);
        return map;
    }

    /**
     * @param obj 对象
     * @return json
     * @throws IOException
     * @Description: 对象转换成json
     */
    public static String translateToJson(Object obj) throws IOException {
        return objectMapper.writeValueAsString(obj);
    }

    /**
     * @param json
     * @param type
     * @return
     * @throws IOException
     * @Description: json 转换成对象
     */
    public static <T> T readValueByType(String json, TypeReference<?> type) throws IOException {
        return objectMapper.readValue(json, type);
    }

    /**
     * @param filePath 文件路径
     * @return
     * @throws
     * @Description: 从文件读取json数据
     */
    public static String readJsonByFile(String filePath) throws Exception {
        File file = new File(filePath);
        InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
        int length;
        StringBuilder sb = new StringBuilder();
        char[] buffer = new char[1024];
        try {
            while ((length = reader.read(buffer, 0, 1024)) != -1) {
                sb.append(buffer, 0, length);
            }
        } finally {
            reader.close();
        }
        return sb.toString();
    }

    /**
     * @Description: 返回前台
     */
    public static void sendJsonToPage(Object obj, HttpServletResponse response) {
        try {
            String json = JsonUtils.translateToJson(obj);
            if (!"[".equalsIgnoreCase(json.substring(0, 1))) {
                json = "[" + json + "]";
            }
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(json);
            response.getWriter().flush();
            response.getWriter().close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
